package com.smartlink.vn.htmlextractor;

import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

public class HtmlExtractor {

    public static boolean validateFshareUrl(String url, boolean checkContainFshare) {
        boolean isValid = false;
        if (checkContainFshare && url.contains("fshare.vn/")) {
            isValid = true;
        }

        if (url.endsWith("T") || url.endsWith("T/")) {
            isValid = true;
        }
        return isValid;
    }

    public static boolean isFshareFolderUrl(String url) {
        return url.contains("/folder/");
    }

    public static void addToList(List<String> urls, String url) {
        if (url != null && !urls.contains(url)) {
            if (url.endsWith("/")) {
                url = url.substring(0, url.length() - 1);
            }
            urls.add(url);
        }
    }

    public static void main(String[] args) throws IOException, Exception {
        if (args == null || args[0].isEmpty()) {
            throw new Exception("Invalid args");
        }
        String link = Constaints.URL1;
        link = args[0];
        boolean hasNextPage;
        Connection conn;
        URL url;
        Document doc;
        Elements elements;
        Map<String, List<String>> result = new HashMap<>();
        System.out.println("Link: " + link);
        List<String> allUrls = new ArrayList<>();
        do {
            List<String> urls = new ArrayList<>();
//            System.out.println("Link: " + link);
            conn = Jsoup.connect(link);
            url = conn.request().url();
            doc = conn.get();
//            System.out.println("a:");
            elements = doc.select("a[href~=fshare.vn/]");
            for (Element element : elements) {
                String value = element.attr("href");
                boolean isValid = validateFshareUrl(value, false);
                if (isValid) {
                    addToList(urls, value);
                    addToList(allUrls, value);
                }
            }

//            System.out.println("pre:");
            elements = doc.select("pre.alt2[dir=ltr]");
            for (Element element : elements) {
                String value = element.text();
                boolean isValid = validateFshareUrl(value, true);
                if (isValid) {
                    addToList(urls, value);
                    addToList(allUrls, value);
                }
            }
            result.put(link, urls);
            elements = doc.select("div.pagenav a[rel=next]");
            hasNextPage = !elements.isEmpty();
            if (hasNextPage) {
                String nextUrlPath = elements.get(0).attr("href");
                link = String.format("%s://%s/diendan/%s", url.getProtocol(), url.getHost(), nextUrlPath);
            }
        } while (hasNextPage);
//        System.out.println("FINISH!");
        System.out.println("Fshare Folders:");
        for (String u : allUrls) {
            if (isFshareFolderUrl(u)) {
                System.out.println(u);
            }
        }
        System.out.println("Fshare Files:");
        for (String u : allUrls) {
            if (!isFshareFolderUrl(u)) {
                System.out.println(u);
            }
        }
    }

}
